﻿
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Core;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.IO;

namespace Yuanlin.Business
{
    public class CompanyMessageEditControl : YuanlinUserControl,
        IReplyConverter<CompanyMessageReply, GetOperation>
    {
        protected ValuedTextBox visitorNameValuedTextBox;
        protected ValuedTextBox addressValuedTextBox;
        protected ValuedTextBox contactValuedTextBox;
        protected ValuedTextBox greenNumberValuedTextBox;
        protected ValuedTextBox bodyValuedTextBox;
        protected HtmlControl divHidden;
        protected ValuedEditor descriptionValuedEditor;

        protected IButton BackButton;
        private CompanyMessage companyMessage;

        public override void GetRequests(List<BaseRequestBinder> requestBinders)
        {
            if (!Page.IsPostBack)
            {
                requestBinders.Add(RequestHelper.BuildGetCompanyMessageRequest(this));
            }
        }
        protected override void OnLoad(EventArgs e)
        {
            BackButton.Click += new EventHandler(BackButton_Click);
        }

        private void Bind()
        {
            if (companyMessage != null)
            {
                if (companyMessage.VisitorUserName.Value != null)
                {
                    visitorNameValuedTextBox.Text = companyMessage.VisitorUserName.Value;
                    addressValuedTextBox.Text = companyMessage.Address.Value;
                    contactValuedTextBox.Text = companyMessage.Contact.Value;
                    greenNumberValuedTextBox.Text = companyMessage.GreenNumber.Value;
                }
                else
                {
                    divHidden.Visible = false;
                }
                this.descriptionValuedEditor.Value = companyMessage.Body.Value;
            }
        }

        public void BackButton_Click(object sender, EventArgs e)
        {
            Page.Response.Redirect(SiteUrls.Instance().GetCompanyMessageListUrl());
        }

        #region IReplyConverter<CompanyMessageReply,GetOperation> Members

        public void ConvertReply(CompanyMessageReply reply, GetOperation operation)
        {
            if (reply != null)
            {
                companyMessage = reply.Entity as CompanyMessage;

                if (!Page.IsPostBack)
                {
                    Bind();
                }
            }
        }

        #endregion
    }
}